<?php
namespace app\admin\controller;

use app\admin\model\Users;

use think\Db;

use think\Controller;
use think\Request;

class User extends Controller
{
	public function index()
	    {   $where=array();
		    $this->assign('uname','');
			if(!empty(input('uname'))){
			  $where['uname'] = input('uname');
			  
			   $this->assign('uname',input('uname'));
			}
			 $list =Db::table('user')->where($where)->order('addtime desc')->paginate(10); 
			// var_dump($list);
		     $page = $list->render();
			 $this->assign('list',$list);
			 $this->assign('page',$page);
			 return $this->fetch(); 
	    }

	public function del($uid){
	   Db::table('user')->delete($uid);
	   $this->success('删除成功');
	}
	
	/**
	 * 用户新增
	 * Enter description here ...
	 * @param Request $request
	 * 
	 */
	public function add(Request $request)
	{
		$user = new Users();
		if($request->isPost()){
			
			$user->uname = $request->param('uname');
			$user->upwd = sha1($request->param('upwd'));
			$user->fid = $request->param('fid');
			$user->cid = $request->param('fid')==0 ? 0 : ($request->param('cid').",".$request->param('fid'));
			$user->level = $request->param('level');
			$user->weixin = $request->param('weixin');
			$user->zfb = $request->param('zfb');
			$user->yhk = $request->param('yhk');
			$user->status = $request->param('status');
			$result = $user->save(); 
			//下面处理：1、将超过6层的从上往下状态（status）修改为0（实效），2、将6层以内的金额各加100元
			$arr = explode(",", $user->cid);
			rsort($arr,SORT_NUMERIC);//将数组进行降序排序
			if(count($arr)>6){//超过6层
				$subArr= array_slice($arr,6);
				$str=implode(",",$subArr);
				echo $str;
				Db::table('user')->where('uid','in',$str)->update(['status'=>0]);
				$sub = array_slice($arr, 0,6);//取前六个
			}else{
				$sub = array_slice($arr, 0);
			}
			$sub=implode(",",$sub);
			//更新他的上级数据
			DB::table("money")->where("uid","IN",$sub)->update([
				'money'=>['exp','money+100'],
				 'addtime'  => ['exp','time()'],
				 'status'=>1
			]);
			//插入自己的数据到money表 $user->uid:自增后的id值
			$data = ['uid' => $user->uid, 'money' =>  $request->param('money'),'addtime'=>time(),'status'=>1];
			Db::table('money')->insert($data);
			
			if($result>0){
				//$this->success("新增成功","admin/User/index"); 
			}else{
				$this->error("新增失败");
			}
		}else{
			//获取最大的一条数据,它的作用仅用来确定当前的层级
			$u = $user->getLastInfo();
			if(is_null($u)){// 如果表中没有数据
				$u['level']=0;
			}
			$lev = $u['level'];//最大id所对应的层数
			
			$parentUser = [];
			//获取该层的所有条数
			$count = $user->getCount($lev);//$l为0时查询结果为0
			if($lev==0){//一条数据没有时，当前层数设为1层
				$nowLevel = 1;
				$parentUser['uid']=0;
				$parentUser['cid']=0;
			}elseif($lev==1){//第二层的第一个元素
				$nowLevel = 2;
				$first = $user->getFirstUser();
				$parentUser['uid']=$first['uid'];
				$parentUser['cid']=0;
			}elseif($count<pow(2,($lev-1))){//从第二层的第二个元素开始
				$nowLevel = $lev;
				$parentUser = $user->getParentUser($lev, 2);//2代表未满
			}else{//当前层已达到最大数值时，转到下一层
				$nowLevel = $lev+1;
				$parentUser = $user->getParentUser($lev, 1);//1.代表已满
			}
			
			return $this->fetch('add',[
				'nowLever'=>$nowLevel,
				'user'=>$parentUser,
			]);
		}
	}
	/**
	 * 用户修改
	 * Enter description here ...
	 * @param Request $request
	 * @param unknown_type $uid
	 */
	public function edit(Request $request,$uid)
	{
		
	}
}

